home *** CD-ROM | disk | FTP | other *** search
/ Gold Medal Software 1 / Gold Medal Software Volume 1 (Gold Medal) (1994).iso / graphics / tierra40.arj / TIERRA / TSETUP.C < prev    next >
C/C++ Source or Header  |  1992-09-09  |  31KB  |  912 lines

  1. /* tsetup.c  9-9-92  Tierra Simulator  setup routines */
  2. /* Tierra Simulator V4.0: Copyright (c) 1991, 1992 Tom Ray & Virtual Life */
  3.  
  4. #ifndef lint
  5. static char sccsid[] = "@(#)tsetup.c    1.5 7/21/92";
  6.  
  7. #endif
  8.  
  9. #include "license.h"
  10. #include "tierra.h"
  11. #include "extern.h"
  12. #include <errno.h>
  13. #include <signal.h>
  14. #ifdef unix
  15. #include <unistd.h>
  16. #endif /* unix */
  17.  
  18. #ifdef MEM_CHK
  19. #include <memcheck.h>
  20. #endif
  21.  
  22. I8s GetAVar(data)
  23.     I8s data[85];
  24. {
  25.     switch (*data) {
  26.     case 'a':
  27.     if (!strncmp(data, "alive", 5))
  28.         return sscanf(data, "alive = %ld", &alive), 1;
  29.     case 'd':
  30.     if (!strncmp(data, "debug", 5))
  31.         return sscanf(data, "debug = %ld", &debug), 1;
  32.     case 'h':
  33.     if (!strncmp(data, "hangup", 6))
  34.         return sscanf(data, "hangup = %ld", &hangup), 1;
  35.     case 'n':
  36.     if (!strncmp(data, "new_soup", 8))
  37.         return sscanf(data, "new_soup = %ld", &new_soup), 1;
  38.     case 's':
  39.     if (!strncmp(data, "seed", 4))
  40.         return sscanf(data, "seed = %ld", &seed), 1;
  41.     case 'B':
  42.     if (!strncmp(data, "BrkupSiz", 8))
  43.         return sscanf(data, "BrkupSiz = %ld", &BrkupSiz), 1;
  44.     case 'C':
  45.     if (!strncmp(data, "CumGeneBnk", 10))
  46.         return sscanf(data, "CumGeneBnk = %ld", &CumGeneBnk), 1;
  47.     case 'D':
  48.     if (!strncmp(data, "DiskOut", 7))
  49.         return sscanf(data, "DiskOut = %ld", &DiskOut), 1;
  50.     if (!strncmp(data, "DistFreq", 8))
  51.         return sscanf(data, "DistFreq = %f", &DistFreq), 1;
  52.     if (!strncmp(data, "DistProp", 8))
  53.         return sscanf(data, "DistProp = %f", &DistProp), 1;
  54.     if (!strncmp(data, "DivSameSiz", 10))
  55.         return sscanf(data, "DivSameSiz = %ld", &DivSameSiz), 1;
  56.     if (!strncmp(data, "DivSameGen", 10))
  57.         return sscanf(data, "DivSameGen = %ld", &DivSameGen), 1;
  58.     if (!strncmp(data, "DropDead", 8))
  59.         return sscanf(data, "DropDead = %ld", &DropDead), 1;
  60.     case 'G':
  61.     if (!strncmp(data, "GeneBnker", 9))
  62.         return sscanf(data, "GeneBnker = %ld", &GeneBnker), 1;
  63.     if (!strncmp(data, "GenebankPath", 12))
  64.         return sscanf(data, "GenebankPath = %s", GenebankPath), 1;
  65.     if (!strncmp(data, "GenPerBkgMut", 12))
  66.         return sscanf(data, "GenPerBkgMut = %f", &GenPerBkgMut), 1;
  67.     if (!strncmp(data, "GenPerFlaw", 10))
  68.         return sscanf(data, "GenPerFlaw = %f", &GenPerFlaw), 1;
  69.     if (!strncmp(data, "GenPerMovMut", 12))
  70.         return sscanf(data, "GenPerMovMut = %f", &GenPerMovMut), 1;
  71.     case 'L':
  72.         if (!strncmp(data, "Log", 3))
  73.             return sscanf(data, "Log = %ld", &Log), 1;
  74.     case 'I':
  75.          if (!strncmp(data, "IMapFile", 8))
  76.            return sscanf(data, "IMapFile = %s", IMapFile), 1;
  77.     case 'M':
  78.     if (!strncmp(data, "MalMode", 7))
  79.         return sscanf(data, "MalMode = %ld", &MalMode), 1;
  80.     if (!strncmp(data, "MalReapTol", 10))
  81.         return sscanf(data, "MalReapTol = %ld", &MalReapTol), 1;
  82.     if (!strncmp(data, "MalTol", 6))
  83.         return sscanf(data, "MalTol = %ld", &MalTol), 1;
  84.     if (!strncmp(data, "MateProb", 8))
  85.         return sscanf(data, "MateProb = %f", &MateProb), 1;
  86.     if (!strncmp(data, "MateSearchL", 11))
  87.         return sscanf(data, "MateSearchL = %ld", &MateSearchL), 1;
  88.     if (!strncmp(data, "MateSizeEp", 10))
  89.         return sscanf(data, "MateSizeEp = %ld", &MateSizeEp), 1;
  90.     if (!strncmp(data, "MateXoverProp", 13))
  91.         return sscanf(data, "MateXoverProp = %f", &MateXoverProp), 1;
  92.     if (!strncmp(data, "MaxFreeBlocks", 13))
  93.         return sscanf(data, "MaxFreeBlocks = %ld", &MaxFreeBlocks), 1;
  94.     if (!strncmp(data, "MemModeFree", 11))
  95.         return sscanf(data, "MemModeFree = %ld", &MemModeFree), 1;
  96.     if (!strncmp(data, "MemModeProt", 11))
  97.         return sscanf(data, "MemModeProt = %ld", &MemModeProt), 1;
  98.     if (!strncmp(data, "MaxMalMult", 10))
  99.         return sscanf(data, "MaxMalMult = %f", &MaxMalMult), 1;
  100.     if (!strncmp(data, "MinCellSize", 11))
  101.         return sscanf(data, "MinCellSize = %ld", &MinCellSize), 1;
  102.     if (!strncmp(data, "MinTemplSize ", 12))
  103.         return sscanf(data, "MinTemplSize = %ld", &MinTemplSize), 1;
  104.     if (!strncmp(data, "MovPropThrDiv", 13))
  105.         return sscanf(data, "MovPropThrDiv = %f", &MovPropThrDiv), 1;
  106.     case 'N':
  107.     if (!strncmp(data, "NumCells", 8))
  108.         return sscanf(data, "NumCells = %ld", &NumCells), 1;
  109.     case 'O':
  110.     if (!strncmp(data, "OutPath", 7))
  111.         return sscanf(data, "OutPath = %s", OutPath), 1;
  112.     case 'P':
  113.     if (!strncmp(data, "PhotonPow", 9))
  114.         return sscanf(data, "PhotonPow = %lf", &PhotonPow), 1;
  115.     if (!strncmp(data, "PhotonWidth", 11))
  116.         return sscanf(data, "PhotonWidth = %ld", &PhotonWidth), 1;
  117.     if (!strncmp(data, "PhotonWord", 10))
  118.         return sscanf(data, "PhotonWord = %s", PhotonWord), 1;
  119.     if (!strncmp(data, "PutLimit", 8))
  120.         return sscanf(data, "PutLimit = %f", &PutLimit), 1;
  121.     case 'R':
  122.     if (!strncmp(data, "RamBankSiz", 10))
  123.         return sscanf(data, "RamBankSiz = %ld", &RamBankSiz), 1;
  124.     if (!strncmp(data, "ReapRndProp", 11))
  125.         return sscanf(data, "ReapRndProp = %g", &ReapRndProp), 1;
  126.     case 'S':
  127.     if (!strncmp(data, "SaveFreq", 8))
  128.         return sscanf(data, "SaveFreq = %ld", &SaveFreq), 1;
  129.     if (!strncmp(data, "SavMinNum", 9))
  130.         return sscanf(data, "SavMinNum = %ld", &SavMinNum), 1;
  131.     if (!strncmp(data, "SavThrMem", 9))
  132.         return sscanf(data, "SavThrMem = %f", &SavThrMem), 1;
  133.     if (!strncmp(data, "SavThrPop", 9))
  134.         return sscanf(data, "SavThrPop = %f", &SavThrPop), 1;
  135.     if (!strncmp(data, "SearchLimit", 11))
  136.         return sscanf(data, "SearchLimit = %f", &SearchLimit), 1;
  137.     if (!strncmp(data, "SizDepSlice", 11))
  138.         return sscanf(data, "SizDepSlice = %ld", &SizDepSlice), 1;
  139.     if (!strncmp(data, "SlicePow", 8))
  140.         return sscanf(data, "SlicePow = %lf", &SlicePow), 1;
  141.     if (!strncmp(data, "SliceSize", 9))
  142.         return sscanf(data, "SliceSize = %ld", &SliceSize), 1;
  143.     if (!strncmp(data, "SliceStyle", 10))
  144.         return sscanf(data, "SliceStyle = %ld", &SliceStyle), 1;
  145.     if (!strncmp(data, "SlicFixFrac", 11))
  146.         return sscanf(data, "SlicFixFrac = %f", &SlicFixFrac), 1;
  147.     if (!strncmp(data, "SlicRanFrac", 11))
  148.         return sscanf(data, "SlicRanFrac = %f", &SlicRanFrac), 1;
  149.     if (!strncmp(data, "SoupSize", 8))
  150.         return sscanf(data, "SoupSize = %ld", &SoupSize), 1;
  151.     case 'W':
  152.     if (!strncmp(data, "WatchExe", 8))
  153.         return sscanf(data, "WatchExe = %ld", &WatchExe), 1;
  154.     if (!strncmp(data, "WatchMov", 8))
  155.         return sscanf(data, "WatchMov = %ld", &WatchMov), 1;
  156.     if (!strncmp(data, "WatchTem", 8))
  157.         return sscanf(data, "WatchTem = %ld", &WatchTem), 1;
  158.     default:
  159.         return 0;
  160.     }
  161. }
  162.  
  163. void GetSoup(argc,argv)
  164. I32s argc;
  165. I8s *argv[];
  166. {
  167.     FILE *inf;
  168.     I32s i;
  169.     SList **tsl;
  170.  
  171.     sprintf(mes[0], "Using instruction set (INST) = %d", INST);
  172.     FEMessage(1,mes);
  173.  
  174. #ifdef __TURBOC__
  175.     timezone = (I32s) 5L *60L * 60L;
  176. #endif /* __TURBOC__ */
  177.  
  178.     if (argc > 1)
  179.     {   sprintf(soup_fn,"%s", argv[1]); }
  180.     else
  181.     {
  182. #ifdef IBM3090
  183.         sprintf(soup_fn,"soup_in%d.io.d", INST);
  184. #else
  185.         sprintf(soup_fn,"soup_in%d", INST);
  186. #endif
  187.     }
  188.  
  189.     inf = fopen(soup_fn, "r");
  190.     if (inf == NULL)
  191.     {   FEError(-1300,EXIT,NOWRITE,
  192.             "Tierra GetSoup() file %s not opened, exiting\n", soup_fn);
  193.     }
  194.     fgets(Buff, 84, inf);
  195.     while (1)
  196.     {   if (*Buff != '#' && strlen(Buff) > 3 && !GetAVar(Buff))
  197.             FEError(-1301,NOEXIT,NOWRITE,
  198.                 "Tierra GetSoup() bad soup_in line: %s\n", Buff);
  199.         fgets(Buff, 84, inf);
  200.         if (*Buff >= '0' && *Buff <= '9' || !strncmp("space",Buff,5))
  201.             break;
  202.     }
  203.     ToggleLog(0L);
  204.     if (!seed)
  205.     {   seed = (I32s) time(NULL);
  206.         tsrand(seed);
  207.         seed = tlrand();
  208.         for (i = tcrand(); i >=0; i--)
  209.             seed = tlrand();
  210.         if (!new_soup)
  211.         {   tsrand(seed);
  212.             tlrand();
  213.         }
  214.     }
  215.     if (new_soup)
  216.     {   tsrand(seed);
  217.         tlrand();
  218.         sprintf(mes[0], "seed = %ld", seed);
  219.         FEMessage(1,mes);
  220.         GenInList = (I8s **) tcalloc((I32u) NumCells, sizeof(I8s *));
  221.         GenInBuf = (I8s *) tcalloc((I32u) NumCells * 30, sizeof(I8s));
  222.         for (i = 0; i < NumCells; i++)
  223.         {   GenInList[i] = GenInBuf + (i * 30);
  224.             sscanf(Buff, "%[^\n]", GenInList[i]);
  225.             fgets(Buff, 84, inf);
  226.         }
  227.     }
  228.  
  229.     /* FEStartup();       */
  230.  
  231.     /* allocate arrays */
  232.     sprintf(mes[0], "sizeof(Instruction)   = %ld",
  233.         (I32s) sizeof(Instruction));
  234.     sprintf(mes[1], "sizeof(Cell)   = %ld",
  235.         (I32s) sizeof(Cell));
  236.     sprintf(mes[2], "sizeof(MemFr) = %ld",
  237.         (I32s) sizeof(MemFr));
  238.     FEMessage(3,mes);
  239. #ifdef __TURBOC__
  240.     sprintf(mes[0], "coreleft = %lu", (I32u) coreleft());
  241.     FEMessage(1,mes);
  242. #endif
  243.     if (new_soup)
  244.     {
  245.         soup = (HpInst) thcalloc((I32u) SoupSize, sizeof(Instruction));
  246.         sprintf(mes[0], "    %ld bytes allocated for soup",
  247.             SoupSize * sizeof(Instruction));
  248.         CelArSiz = CellsSize = (I32s) 16384 / (I32s) sizeof(Cell);
  249.         NumCelAr = 1;
  250.         if (NumCells > CellsSize - 2)
  251.         {   NumCelAr = (NumCells + 2) / CelArSiz;
  252.             if((NumCells + 2) % CelArSiz)
  253.                 NumCelAr++;
  254.             CellsSize = NumCelAr * CelArSiz;
  255.         }
  256.         cells = (Pcells  Fp) tcalloc(NumCelAr, sizeof(Pcells));
  257.         for (i = 0; i < NumCelAr; i++)
  258.             cells[i] = (Pcells) tcalloc(CelArSiz, sizeof(Cell));
  259.         sprintf(mes[1], "    %ld bytes allocated for cells",
  260.             CellsSize * sizeof(Cell));
  261.         TopDummy =    &cells[0][0];
  262.         BottomDummy = &cells[0][1];
  263.         FreeMem = (MemFr Fp) tcalloc(MaxFreeBlocks, sizeof(MemFr));
  264.         sprintf(mes[2], "    %ld bytes allocated for MemFr",
  265.             MaxFreeBlocks * sizeof(MemFr));
  266.         FEMessage(3,mes);
  267. #ifdef __TURBOC__
  268.         sprintf(mes[0], "coreleft = %lu  tsetup (soup, cells, FreeMem)",
  269.             coreleft());
  270.         FEMessage(1,mes);
  271. #endif
  272.         if (!soup || !cells || !FreeMem)
  273.             FEError(-1302,EXIT,NOWRITE,
  274.                 "Tierra GetSoup() setup malloc error, exiting\n");
  275.         else
  276.         {   sprintf(mes[0], "tsetup: arrays allocated without error");
  277.             FEMessage(1,mes);
  278.         }
  279.     }
  280.     TotFlaw = TotMovMut = TotMut = isolate = 0;
  281.     extr.a = extr.i = 0;
  282.     Disturb.m = Disturb.i = DistNext.m = DistNext.i = 0L;
  283.     siz_sl = 1;
  284.     if (DivSameGen)
  285.         DivSameSiz = 1;
  286.     if (GeneBnker)
  287.     {   tsl = (SList **) tcalloc(siz_sl, sizeof(SList *));
  288.         if (tsl == NULL)
  289.            FEError(-1303,EXIT,NOWRITE,"Tierra GetSoup() tsl tcalloc error\n");
  290.         else
  291.             sl = tsl;
  292.     }
  293.     else
  294.         WatchExe = WatchMov = WatchTem = 0;
  295.     if (new_soup)
  296.         GetNewSoup();
  297.     else
  298.         GetOldSoup(inf);
  299.     sprintf(mes[0], "tsetup: soup gotten");
  300.     FEMessage(1,mes);
  301.     if (SliceStyle == 1)
  302.     {   PhotonSize = (I32s) strlen(PhotonWord);
  303.         PhotonTranslate(PhotonInst, PhotonWord);
  304.         slicer = SlicerPhoton;
  305.     }
  306.     else if (SliceStyle == 0)
  307.         slicer = SlicerQueue;
  308.     else /* default SliceStyle == 2 */
  309.         slicer = RanSlicerQueue;
  310.     if (new_soup)
  311.     {   if (GenInList)
  312.         {   tfree(GenInList);
  313.             GenInList = NULL;
  314.         }
  315.         if (GenInBuf)
  316.         {   tfree(GenInBuf);
  317.             GenInBuf = NULL;
  318.         }
  319.     }
  320.     fclose(inf);
  321.     if(strncmp(IMapFile,"-.map",5) ) /* not the default inst map */
  322.     {   sprintf(Buff,"%s%s",GenebankPath,IMapFile);
  323.         GetAMap(Buff);
  324.     }
  325. /* added to GetSoup from old tierra.c */
  326. #ifdef __TURBOC__
  327. #if FRONTEND == STDIO
  328.     ctrlbrk( T_sig_int);
  329. #endif
  330. #if FRONTEND == BASIC
  331. FEPrintf(HLP_X,HLP_Y,1,
  332. "                         Press Any Key for menu ...                        "
  333. );
  334. #endif  /* BASIC */
  335. #endif    /* __TURBOC__ */
  336.  
  337. #ifdef unix
  338.     signal(SIGINT,  T_sig_int);
  339.     signal(SIGUSR1, T_sig_read);
  340.     signal(SIGUSR2, T_sig_info);
  341.     signal(SIGHUP,  T_sig_write);
  342.     SigBlockSet = sigmask(SIGINT);
  343. #endif
  344.  
  345. }
  346.  
  347. void GetNewSoup()
  348. {   I32s i, j, ar = 0, ci = 2, cs, sad = 0, tNumCells = NumCells;
  349.     Pcells  ce;
  350.     FILE *inf;
  351.  
  352.     NumCells = 0;
  353.     sprintf(mes[0], "beginning of GetNewSoup");
  354.     FEMessage(1,mes);
  355.     reaped = InstExe.i = InstExe.m = ExtractCount = CountMovMut = 0;
  356.     CountMutRate = CountFlaw = RateMovMut = RateMut = RateFlaw = 0;
  357.     FreeBlocks = FirstOutDisk = 1L;
  358.     Generations = 0.;
  359.     FreeMemCurrent = SoupSize;
  360.     sprintf(mes[0], "init of soup complete");
  361.     FEMessage(1,mes);
  362.     /* initialize FreeMem array */
  363.     MemInit();
  364.     /* initialize cells array */
  365.     for (i = 0; i < NumCelAr; i++)
  366.         for (j = 0; j < CelArSiz; j++)
  367.             InitCell(i,j,&cells[i][j]);
  368.     TopDummy->ld = BottomDummy->ld = 1;
  369.     ce = ThisSlice = BottomReap = TopReap = &cells[0][2];
  370.  
  371.     /* read in the cell genotypes */
  372.     for (i = 0; i < tNumCells; i++)
  373.     {   if (!strncmp("space", GenInList[i], 5))
  374.         {   sscanf(GenInList[i], "%*s%ld", &cs);
  375.             sprintf(mes[0], "skipping %ld instructions", cs);
  376.             FEMessage(1,mes);
  377.         }
  378.         else
  379.         {   sscanf(GenInList[i], "%4ld", &cs);
  380.             sprintf(mes[0], "GetNewSoup: loading %s into cell %ld,%ld",
  381.                 GenInList[i], ar, ci);
  382.             FEMessage(1,mes);
  383.             InjectFromBank(GenInList[i], sad, 0);
  384.             ci++;
  385.         }
  386.         if (!(ci % CelArSiz))
  387.         {   ci = 0;
  388.             ar++;
  389.         }
  390.         sad += cs;
  391.     }
  392.     plan();
  393. }
  394.  
  395. void GetOldSoup(inf)
  396.     FILE *inf;
  397. {   I32s i, gi, si, ar, ci, tNumGenotypes = 0, tNumSizes = 0;
  398.     CellInd  brind, trind, tsind;
  399.     Pcells  ce;
  400.     FILE *inc;
  401.  
  402.     fgets(Buff, 84, inf);
  403.     fgets(Buff, 84, inf);
  404.     sscanf(Buff, "AverageSize = %ld", &AverageSize);
  405.     fgets(Buff, 84, inf);
  406.     sscanf(Buff, "BrkupCou = %ld", &BrkupCou);
  407.     fgets(Buff, 84, inf);
  408.     sscanf(Buff, "BrkupCum = %ld", &BrkupCum);
  409.     fgets(Buff, 84, inf);
  410.     sscanf(Buff, "CelArSiz = %ld", &CelArSiz);
  411.     fgets(Buff, 84, inf);
  412.     sscanf(Buff, "CellsSize = %ld", &CellsSize);
  413.     fgets(Buff, 84, inf);
  414.     sscanf(Buff, "CountFlaw = %ld", &CountFlaw);
  415.     fgets(Buff, 84, inf);
  416.     sscanf(Buff, "CountMovMut = %ld", &CountMovMut);
  417.     fgets(Buff, 84, inf);
  418.     sscanf(Buff, "CountMutRate = %ld", &CountMutRate);
  419.     fgets(Buff, 84, inf);
  420.     sscanf(Buff, "debug_switch = %ld", &debug_switch);
  421.     fgets(Buff, 84, inf);
  422.     sscanf(Buff, "DistNext.m = %ld", &DistNext.m);
  423.     fgets(Buff, 84, inf);
  424.     sscanf(Buff, "DistNext.i = %ld", &DistNext.i);
  425.     fgets(Buff, 84, inf);
  426.     sscanf(Buff, "Disturb.m = %ld", &Disturb.m);
  427.     fgets(Buff, 84, inf);
  428.     sscanf(Buff, "Disturb.i = %ld", &Disturb.i);
  429.     fgets(Buff, 84, inf);
  430.     sscanf(Buff, "ExtractCount = %ld", &ExtractCount);
  431.     fgets(Buff, 84, inf);
  432.     sscanf(Buff, "FirstOutDisk = %ld", &FirstOutDisk);
  433.     fgets(Buff, 84, inf);
  434.     sscanf(Buff, "FreeBlocks = %ld", &FreeBlocks);
  435.     fgets(Buff, 84, inf);
  436.     sscanf(Buff, "FreeMemCurrent = %ld", &FreeMemCurrent);
  437.     fgets(Buff, 84, inf);
  438.     sscanf(Buff, "Generations = %lf", &Generations);
  439.     fgets(Buff, 84, inf);
  440.     sscanf(Buff, "InstExe.i = %ld", &InstExe.i);
  441.     fgets(Buff, 84, inf);
  442.     sscanf(Buff, "InstExe.m = %ld", &InstExe.m);
  443.     fgets(Buff, 84, inf);
  444.     sscanf(Buff, "isolate = %ld", &isolate);
  445.     fgets(Buff, 84, inf);
  446.     sscanf(Buff, "LastDiv.i = %ld", &LastDiv.i);
  447.     fgets(Buff, 84, inf);
  448.     sscanf(Buff, "LastDiv.m = %ld", &LastDiv.m);
  449.     fgets(Buff, 84, inf);
  450.     sscanf(Buff, "MalLimit = %ld", &MalLimit);
  451.     fgets(Buff, 84, inf);
  452.     sscanf(Buff, "NumCelAr = %ld", &NumCelAr);
  453.     fgets(Buff, 84, inf);
  454.     sscanf(Buff, "NumGenotypes = %ld", &NumGenotypes);
  455.     fgets(Buff, 84, inf);
  456.     sscanf(Buff, "NumSizes = %ld", &NumSizes);
  457.     fgets(Buff, 84, inf);
  458.     sscanf(Buff, "Put_limit = %ld", &Put_limit);
  459.     fgets(Buff, 84, inf);
  460.     sscanf(Buff, "RandIx1 = %ld", &RandIx1);
  461.     fgets(Buff, 84, inf);
  462.     sscanf(Buff, "RandIx2 = %ld", &RandIx2);
  463.     fgets(Buff, 84, inf);
  464.     sscanf(Buff, "RandIx3 = %ld", &RandIx3);
  465.     fgets(Buff, 84, inf);
  466.     sscanf(Buff, "RateFlaw = %ld", &RateFlaw);
  467.     fgets(Buff, 84, inf);
  468.     sscanf(Buff, "RateMovMut = %ld", &RateMovMut);
  469.     fgets(Buff, 84, inf);
  470.     sscanf(Buff, "RateMut = %ld", &RateMut);
  471.     fgets(Buff, 84, inf);
  472.     sscanf(Buff, "reaped = %ld", &reaped);
  473.     fgets(Buff, 84, inf);
  474.     sscanf(Buff, "Search_limit = %ld", &Search_limit);
  475.     fgets(Buff, 84, inf);
  476.     sscanf(Buff, "SoupBot = %ld", &SoupBot);
  477.     fgets(Buff, 84, inf);
  478.     sscanf(Buff, "SoupTop = %ld", &SoupTop);
  479.     fgets(Buff, 84, inf);
  480.     sscanf(Buff, "TimeBirth = %ld", &TimeBirth);
  481.     fgets(Buff, 84, inf);
  482.     sscanf(Buff, "TimeDeath = %ld", &TimeDeath);
  483.     fgets(Buff, 84, inf);
  484.     sscanf(Buff, "TimePop = %lf", &TimePop);
  485.     fgets(Buff, 84, inf);
  486.     sscanf(Buff, "TotFlaw = %ld", &TotFlaw);
  487.     fgets(Buff, 84, inf);
  488.     sscanf(Buff, "TotMovMut = %ld", &TotMovMut);
  489.     fgets(Buff, 84, inf);
  490.     sscanf(Buff, "TotMut = %ld", &TotMut);
  491.     fgets(Buff, 84, inf);
  492.     sscanf(Buff, "filepos = %ld", &pos);
  493. #ifdef IBM3090
  494.     strcpy(Buff, "core_out.io.d");
  495. #else
  496.     strcpy(Buff, "core_out");
  497. #endif
  498.     ReadDynMem(Buff);
  499.     ReadGeneBank();
  500.     if (DiskOut)
  501.     {
  502. #ifdef IBM3090
  503.         if (BrkupSiz)
  504.             sprintf(Buff, "break.%ld.d", BrkupCou);
  505.         else
  506.             sprintf(Buff, "tierra.run");
  507. #else
  508.         if (BrkupSiz)
  509.             sprintf(Buff, "%sbreak.%ld", OutPath, BrkupCou);
  510.         else
  511.             sprintf(Buff, "%stierra.run", OutPath);
  512. #endif
  513.         oufr = fopen(Buff, "r+");
  514.         if (oufr == NULL)
  515.         {   FEError(-1309,EXIT,NOWRITE,
  516.                 "Tierra GetOldSoup() 1 file %s not opened, exiting", Buff);
  517.         }
  518.         fseek(oufr, pos, SEEK_SET);
  519.     }
  520. }
  521.  
  522. void WriteSoup(close_disk)
  523.     I32s close_disk;
  524. {
  525.     FILE    *ouf;
  526.     I32s    i, j, pos;
  527.     I8s     comd[120], path[99], tpath[99];
  528.     GList   *tgl;
  529.     FILE    *fp, *tf;
  530.     head_t  head, thead;
  531.     indx_t  *indx, *tindx, indxt, gindx;
  532.     long int tp;
  533.  
  534.     if (DiskOut)
  535.     {   pos = ftell(oufr);
  536.         if (close_disk)
  537.             fclose(oufr);
  538.     }
  539.     if (GeneBnker)
  540.         SavGeneBank();
  541.     new_soup = 0;
  542. #ifdef IBM3090
  543.     sprintf(comd, "soup_out.io.d");
  544. #else
  545.     sprintf(comd, "soup_out");
  546. #endif
  547.     tp = (I32s) time(NULL);
  548.     ouf = fopen(comd, "w");
  549.     if (ouf == NULL)
  550.     {   FEError(-1310,EXIT,NOWRITE,
  551.             "Tierra WriteSoup() 0 file %s not opened, exiting", comd);
  552.     }
  553.     fprintf(ouf, "# tierra core:  %s\n", ctime(&tp));
  554.     fprintf(ouf, "# observational parameters:\n\n");
  555.  
  556.     fprintf(ouf, "BrkupSiz = %ld\n", BrkupSiz);
  557.     fprintf(ouf, "CumGeneBnk = %ld\n", CumGeneBnk);
  558.     fprintf(ouf, "debug = %ld\n", debug);
  559.     fprintf(ouf, "DiskOut = %ld\n", DiskOut);
  560.     fprintf(ouf, "GeneBnker = %ld\n", GeneBnker);
  561.     fprintf(ouf, "GenebankPath = %s\n", GenebankPath);
  562.     fprintf(ouf, "hangup = %ld\n", hangup);
  563.     fprintf(ouf, "Log = %ld\n", Log);
  564.     fprintf(ouf, "MaxFreeBlocks = %ld\n", MaxFreeBlocks);
  565.     fprintf(ouf, "OutPath = %s\n", OutPath);
  566.     fprintf(ouf, "RamBankSiz = %ld\n", RamBankSiz);
  567.     fprintf(ouf, "SaveFreq = %ld\n", SaveFreq);
  568.     fprintf(ouf, "SavMinNum = %ld\n", SavMinNum);
  569.     fprintf(ouf, "SavThrMem = %g\n", SavThrMem);
  570.     fprintf(ouf, "SavThrPop = %g\n", SavThrPop);
  571.     fprintf(ouf, "WatchExe = %ld\n", WatchExe);
  572.     fprintf(ouf, "WatchMov = %ld\n", WatchMov);
  573.     fprintf(ouf, "WatchTem = %ld\n", WatchTem);
  574.  
  575.     fprintf(ouf, "\n# environmental variables:\n\n");
  576.  
  577.     fprintf(ouf, "alive = %ld\n", alive);
  578.     fprintf(ouf, "DistFreq = %f\n", DistFreq);
  579.     fprintf(ouf, "DistProp = %f\n", DistProp);
  580.     fprintf(ouf, "DivSameSiz = %ld\n", DivSameSiz);
  581.     fprintf(ouf, "DivSameGen = %ld\n", DivSameGen);
  582.     fprintf(ouf, "DropDead = %ld\n", DropDead);
  583.     fprintf(ouf, "GenPerBkgMut = %f\n", GenPerBkgMut);
  584.     fprintf(ouf, "GenPerFlaw = %f\n", GenPerFlaw);
  585.     fprintf(ouf, "GenPerMovMut = %f\n", GenPerMovMut);
  586.     fprintf(ouf, "IMapFile = %s\n", IMapFile);
  587.     fprintf(ouf, "MalMode = %ld\n", MalMode);
  588.     fprintf(ouf, "MalReapTol = %ld\n", MalReapTol);
  589.     fprintf(ouf, "MalTol = %ld\n", MalTol);
  590.     fprintf(ouf, "MateProb = %f\n", MateProb);
  591.     fprintf(ouf, "MateSearchL = %ld\n", MateSearchL);
  592.     fprintf(ouf, "MateSizeEp = %ld\n", MateSizeEp);
  593.     fprintf(ouf, "MateXoverProp = %f\n", MateXoverProp);
  594.     fprintf(ouf, "MaxMalMult = %g\n", MaxMalMult);
  595.     fprintf(ouf, "MinCellSize = %ld\n", MinCellSize);
  596.     fprintf(ouf, "MinTemplSize = %ld\n", MinTemplSize);
  597.     fprintf(ouf, "MovPropThrDiv = %g\n", MovPropThrDiv);
  598.     fprintf(ouf, "MemModeFree = %ld\n", MemModeFree);
  599.     fprintf(ouf, "MemModeProt = %ld\n", MemModeProt);
  600.     fprintf(ouf, "new_soup = %ld\n", new_soup);
  601.     fprintf(ouf, "NumCells = %ld\n", NumCells);
  602.     fprintf(ouf, "PhotonPow = %g\n", PhotonPow);
  603.     fprintf(ouf, "PhotonWidth = %ld\n", PhotonWidth);
  604.     fprintf(ouf, "PhotonWord = %s\n", PhotonWord);
  605.     fprintf(ouf, "PutLimit = %g\n", PutLimit);
  606.     fprintf(ouf, "ReapRndProp = %g\n", ReapRndProp);
  607.     fprintf(ouf, "SearchLimit = %g\n", SearchLimit);
  608.     fprintf(ouf, "seed = %ld\n", seed);
  609.     fprintf(ouf, "SizDepSlice = %ld\n", SizDepSlice);
  610.     fprintf(ouf, "SlicePow = %g\n", SlicePow);
  611.     fprintf(ouf, "SliceSize = %ld\n", SliceSize);
  612.     fprintf(ouf, "SliceStyle = %ld\n", SliceStyle);
  613.     fprintf(ouf, "SlicFixFrac = %g\n", SlicFixFrac);
  614.     fprintf(ouf, "SlicRanFrac = %g\n", SlicRanFrac);
  615.     fprintf(ouf, "SoupSize = %ld\n", SoupSize);
  616.  
  617.     fprintf(ouf, "\n0 to stop GetAVar\n\n");
  618.     /* end soup_in variables */
  619.  
  620.     fprintf(ouf, "AverageSize = %ld\n", AverageSize);
  621.     fprintf(ouf, "BrkupCou = %ld\n", BrkupCou);
  622.     fprintf(ouf, "BrkupCum = %ld\n", BrkupCum);
  623.     fprintf(ouf, "CelArSiz = %ld\n", CelArSiz);
  624.     fprintf(ouf, "CellsSize = %ld\n", CellsSize);
  625.     fprintf(ouf, "CountFlaw = %ld\n", CountFlaw);
  626.     fprintf(ouf, "CountMovMut = %ld\n", CountMovMut);
  627.     fprintf(ouf, "CountMutRate = %ld\n", CountMutRate);
  628.     fprintf(ouf, "debug_switch = %ld\n", debug_switch);
  629.     fprintf(ouf, "DistNext.m = %ld\n", DistNext.m);
  630.     fprintf(ouf, "DistNext.i = %ld\n", DistNext.i);
  631.     fprintf(ouf, "Disturb.m = %ld\n", Disturb.m);
  632.     fprintf(ouf, "Disturb.i = %ld\n", Disturb.i);
  633.     fprintf(ouf, "ExtractCount = %ld\n", ExtractCount);
  634.     fprintf(ouf, "FirstOutDisk = %ld\n", FirstOutDisk);
  635.     fprintf(ouf, "FreeBlocks = %ld\n", FreeBlocks);
  636.     fprintf(ouf, "FreeMemCurrent = %ld\n", FreeMemCurrent);
  637.     fprintf(ouf, "Generations = %lf\n", Generations);
  638.     fprintf(ouf, "InstExe.i = %ld\n", InstExe.i);
  639.     fprintf(ouf, "InstExe.m = %ld\n", InstExe.m);
  640.     fprintf(ouf, "isolate = %ld\n", isolate);
  641.     fprintf(ouf, "LastDiv.i = %ld\n", LastDiv.i);
  642.     fprintf(ouf, "LastDiv.m = %ld\n", LastDiv.m);
  643.     fprintf(ouf, "MalLimit = %ld\n", MalLimit);
  644.     fprintf(ouf, "NumCelAr = %ld\n", NumCelAr);
  645.     fprintf(ouf, "NumGenotypes = %ld\n", NumGenotypes);
  646.     fprintf(ouf, "NumSizes = %ld\n", NumSizes);
  647.     fprintf(ouf, "Put_limit = %ld\n", Put_limit);
  648.     fprintf(ouf, "RandIx1 = %ld\n", RandIx1);
  649.     fprintf(ouf, "RandIx2 = %ld\n", RandIx2);
  650.     fprintf(ouf, "RandIx3 = %ld\n", RandIx3);
  651.     fprintf(ouf, "RateFlaw = %ld\n", RateFlaw);
  652.     fprintf(ouf, "RateMovMut = %ld\n", RateMovMut);
  653.     fprintf(ouf, "RateMut = %ld\n", RateMut);
  654.     fprintf(ouf, "reaped = %ld\n", reaped);
  655.     fprintf(ouf, "Search_limit = %ld\n", Search_limit);
  656.     fprintf(ouf, "SoupBot = %ld\n", SoupBot);
  657.     fprintf(ouf, "SoupTop = %ld\n", SoupTop);
  658.     fprintf(ouf, "TimeBirth = %ld\n", TimeBirth);
  659.     fprintf(ouf, "TimeDeath = %ld\n", TimeDeath);
  660.     fprintf(ouf, "TimePop = %lf\n", TimePop);
  661.     fprintf(ouf, "TotFlaw = %ld\n", TotFlaw);
  662.     fprintf(ouf, "TotMovMut = %ld\n", TotMovMut);
  663.     fprintf(ouf, "TotMut = %ld\n", TotMut);
  664.     fprintf(ouf, "filepos = %ld\n", pos);
  665.  
  666.     fclose(ouf);
  667.  
  668. #ifdef IBM3090
  669.     strcpy(comd, "core_out.io.d");
  670. #else
  671.     strcpy(comd, "core_out");
  672. #endif
  673.     SavDynMem(comd);
  674. }
  675.  
  676. void SavDynMem(filename)
  677. I8s  filename[];
  678. {   I32s    i, j;
  679.     I8s     path[80], tpath[80];
  680.     GList   *tgl;
  681.     FILE    *fp, *tf, *ouf;
  682.     head_t  head, thead;
  683.     indx_t  *indx, *tindx, indxt, gindx;
  684.     CellInd  cind[3];
  685.  
  686.     ouf = fopen(filename, "wb");
  687.     if (ouf == NULL)
  688.     {   FEError(-1311,EXIT,NOWRITE,
  689.             "Tierra SavDynMem() file %s not opened, exiting", filename);
  690.     }
  691.     tfwrite((I8s Hp) soup, sizeof(Instruction), SoupSize, ouf);
  692.     if (DiskOut)
  693.         fwrite(&lo, sizeof(LastOut), 1, ouf);
  694.     fwrite(&is, sizeof(PInst), 1, ouf);
  695.     fwrite(TrandArray, sizeof(double), 98, ouf);
  696.     cind[0].a = BottomReap->q.this.a;
  697.     cind[0].i = BottomReap->q.this.i;
  698.     cind[1].a = TopReap->q.this.a;
  699.     cind[1].i = TopReap->q.this.i;
  700.     cind[2].a = ThisSlice->q.this.a;
  701.     cind[2].i = ThisSlice->q.this.i;
  702.     fwrite(cind, sizeof(CellInd), 3, ouf);
  703.     for (i = 0; i < NumCelAr; i++)
  704.         fwrite((I8s Fp) cells[i], sizeof(Cell), CelArSiz, ouf);
  705.     tfwrite((I8s Hp) FreeMem, sizeof(MemFr), MaxFreeBlocks, ouf);
  706.     fclose(ouf);
  707. }
  708.  
  709. void ReadDynMem(filename)
  710. I8s  filename[];
  711. {   I32s     i;
  712.     CellInd  cind[3];
  713.     FILE     *inf;
  714.  
  715.     inf = fopen(filename, "rb");
  716.     if (inf == NULL)
  717.     {   FEError(-1311,EXIT,NOWRITE,
  718.             "Tierra ReadDynMem() file %s not opened, exiting", filename);
  719.     }
  720.     soup = (HpInst) thcalloc((I32u) SoupSize, sizeof(Instruction));
  721.     tfread((I8s Hp) soup, sizeof(Instruction), SoupSize, inf);
  722.     if (DiskOut)
  723.         fread(&lo, sizeof(LastOut), 1, inf);
  724.     fread(&is, sizeof(PInst), 1, inf);
  725.     fread(TrandArray, sizeof(double), 98, inf);
  726.     fread(cind, sizeof(CellInd), 3, inf);
  727.     cells = (Pcells  *) tcalloc(NumCelAr, sizeof(Pcells));
  728.     for (i = 0; i < NumCelAr; i++)
  729.     {   cells[i] = (Pcells) tcalloc(CelArSiz, sizeof(Cell));
  730.         fread((I8s Fp) cells[i], sizeof(Cell), CelArSiz, inf);
  731.     }
  732.     FreeMem = (MemFr Fp) tcalloc(MaxFreeBlocks, sizeof(MemFr));
  733.     tfread((I8s Hp) FreeMem, sizeof(MemFr), MaxFreeBlocks, inf);
  734.     fclose(inf);
  735.  
  736.     TopDummy    = &cells[0][0];
  737.     BottomDummy = &cells[0][1];
  738.     BottomReap  = &cells[cind[0].a][cind[0].i];
  739.     TopReap     = &cells[cind[1].a][cind[1].i];
  740.     ThisSlice   = &cells[cind[2].a][cind[2].i];
  741. }
  742.  
  743. void ReadGeneBank()
  744. {   I32s    gi, tgi, si, ar, ci, tNumGenotypes = 0, tNumSizes = 0;
  745.     Pcells  ce;
  746.     GlIndex  GiHash;
  747.  
  748.     for (ar = 0; ar < NumCelAr; ar++)
  749.         for (ci = 0; ci < CelArSiz; ci++)
  750.     {   ce = &cells[ar][ci];
  751.         if (!ce->ld || (ar == 0 && ci < 2))
  752.             continue;
  753.         ce->d.genome = soup + ce->mm.p;
  754.         GiHash = CheckGenotype(ce->d, 15); /* check .gen and .tmp files */
  755. #ifdef ERROR                               /* and preserve bit 1 */
  756.         if (ce->d.hash != GiHash.si)
  757.             FEError(-1308,EXIT,NOWRITE,
  758.                 "ReadGeneBank(): (ce->d.hash = %ld) != (GiHash.si = %ld)\n",
  759.                 ce->d.hash, GiHash.si);
  760.         if (GiHash.gi != (tgi = Lbl2Int(ce->d.gen.label)))
  761.             FEError(-1308,EXIT,NOWRITE,
  762.              "ReadGeneBank(): ce->d.gen.label = %s = %ld, GiHash.gi = %hd\n",
  763.                 ce->d.gen.label, tgi, GiHash.gi);
  764.         if (GiHash.gi != ce->d.gi)
  765.             FEError(-1308,EXIT,NOWRITE,
  766.              "ReadGeneBank(): ce->d.gi = %hd, GiHash.gi = %hd\n",
  767.                 ce->d.gi, GiHash.gi);
  768. #endif /* ERROR */
  769.         gi = ce->d.gi = GiHash.gi;
  770.         si = ce->d.gen.size;
  771.         strcpy(ce->d.gen.label, Int2Lbl(GiHash.gi));
  772.         if (!sl[si]->g[gi]->pop)
  773.         {   tNumGenotypes++;
  774.             sl[si]->num_g++;
  775.         }
  776.         sl[si]->g[gi]->pop++;
  777.         if (!sl[si]->num_c)
  778.             tNumSizes++;
  779.         sl[si]->num_c++;
  780.     }
  781. #ifdef ERROR
  782.     if(tNumGenotypes != NumGenotypes)
  783.         FEError(-1308,NOEXIT,NOWRITE,
  784.     "Tierra ReadGeneBank() error: tNumGenotoypes = %ld  NumGenotypes = %ld\n",
  785.             tNumGenotypes, NumGenotypes);
  786.     if(tNumSizes != NumSizes)
  787.        FEError(-1308,NOEXIT,NOWRITE,
  788.            "Tierra ReadGeneBank() error: tNumSizes = %ld  NumSizes = %ld\n",
  789.            tNumSizes, NumSizes);
  790. #endif /* ERROR */
  791. }
  792.  
  793. void SavGeneBank()
  794. {   I32s    i, j;
  795.     I8s     path[80], tpath[80];
  796.     GList   *tgl;
  797.     FILE    *fp, *tf;
  798.     head_t  head, thead;
  799.     indx_t  *indx, *tindx, indxt, gindx;
  800.  
  801.     for (i = 0; i < siz_sl; i++)
  802.     {   if (!sl[i] || !sl[i]->num_c)
  803.             continue;
  804.         sprintf(path, "%s%04ld.gen", GenebankPath, i);
  805.         sprintf(tpath, "%s%04ld.tmp", GenebankPath, i);
  806.         fp = open_ar(path, i, GFormat, -1);
  807.         tf = open_ar(tpath, i, GFormat, 1);
  808.         head = read_head(fp);
  809.         thead = read_head(tf);
  810.  
  811. #ifdef __TURBOC__
  812.         indx = &gindx;
  813.         tindx = &indxt;
  814. #else  /* __TURBOC__ */
  815.         indx = read_indx(fp, &head);
  816.         tindx = read_indx(tf, &thead);
  817. #endif /* __TURBOC__ */
  818.  
  819.         /* for each genotype of this size */
  820.         for (j = 0; j < sl[i]->a_num; j++)
  821.         {   if ((I32u) (tgl = sl[i]->g[j]) > 4)
  822.             {   if (IsBit(tgl->bits, 0)) /* if permanent genotype name */
  823.                     add_gen(fp, &head, &indx, tgl);
  824.                 else if (tgl->pop)    /* or has a residual population */
  825.                     add_gen(tf, &thead, &tindx, tgl);
  826.             }
  827.         }
  828.         fclose(fp); fclose(tf);
  829.         if (!head.n)
  830.             unlink(path);
  831.         if (!thead.n)
  832.             unlink(tpath);
  833. #ifndef __TURBOC__
  834.         if (indx)
  835.         {   thfree(indx);
  836.             indx = NULL;
  837.         }
  838.         if (tindx)
  839.         {   thfree(tindx);
  840.             tindx = NULL;
  841.         }
  842. #endif /* __TURBOC__ */
  843.     }
  844. }
  845.  
  846. void InitCell(ar, ci, ce)
  847.     I32s    ar, ci;
  848.     Pcells  ce;
  849. {   I16s  i;
  850. #ifdef unix
  851.     bzero(ce, sizeof(Cell));
  852. #endif               /* unix */
  853. #ifdef __TURBOC__
  854.     memset(ce, 0, sizeof(Cell));
  855. #endif               /* __TURBOC__ */
  856.     strcpy(ce->d.gen.label, "---");
  857.     strcpy(ce->d.parent.label, "---");
  858.     ce->d.gi = (I32s) -1;    /* index to genome in bank */
  859.     ce->d.ploidy = (I8s) 1;    /* how many tracks */
  860.     ce->q.this.a = ar; ce->q.this.i = ci;
  861.     ce->d.ne = ce->q.n_time = ce->q.p_time = ce->q.n_reap
  862.         = ce->q.p_reap = ce->q.this;
  863.     ce->c.sp = (I16s) STACK_SIZE - 1;
  864. #if INST == 2
  865.     for (i = 0; i < NUMREG; i++)
  866.         ce->c.re[NUMREG + i] = i;
  867. #endif /* INST == 2 */
  868. }
  869.  
  870. #ifdef FUTURE
  871.  
  872. void FreeDynMem()
  873. {   I32s  i, j;
  874.  
  875.     if (GeneBnker)
  876.     {   for (i = 0; i < siz_sl; i++) /* for each size */
  877.         {   if (!sl[i])
  878.                 continue;       /* for each genotype of this size */
  879.             for (j = 0; j < sl[i]->a_num; j++)
  880.             {   if ((I32u) (sl[i]->g[j]) > 4)
  881.                 {   tfree(sl[i]->g[j]);
  882.                     sl[i]->g[j] = NULL;
  883.                 }
  884.             }
  885.             tfree(sl[i]->g);
  886.             sl[i]->g = NULL;
  887.         }
  888.         tfree(sl);
  889.         sl = NULL;
  890.     }
  891.     if (soup)
  892.     {   thfree(soup);
  893.         soup = NULL;
  894.     }
  895.     for (i = 0; i < NumCelAr; i++)
  896.     {   if (cells[i])
  897.         {   tfree(cells[i]);
  898.             cells[i] = NULL;
  899.         }
  900.     }
  901.     if (cells)
  902.     {   tfree(cells);
  903.         cells = NULL;
  904.     }
  905.     if (FreeMem)
  906.     {   tfree(FreeMem);
  907.         FreeMem = NULL;
  908.     }
  909. }
  910.  
  911. #endif /* FUTURE */
  912.